package com.gxar.quick.ar.server.base.model.combo.mapper;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gxar.quick.ar.server.base.common.enums.CurrencyEnum;
import com.gxar.quick.ar.server.base.model.combo.vo.ComboConfigInfoVO;
import com.gxar.quick.ar.server.base.model.combo.vo.response.ComboListResponse;
import com.gxar.quick.ar.server.db.model.ComboEntity;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/11/9 17:25
 */
@Mapper
public interface ComboMapStruct {
    ComboMapStruct INSTANCE = Mappers.getMapper(ComboMapStruct.class);


    @Mapping(target = "id", expression = "java(String.valueOf(entity.getId()))")
    @Mapping(target = "currency", qualifiedByName = "currencyConvert")
    @Mapping(target = "config", qualifiedByName = "configConvert")
    ComboListResponse entityToListResponse(ComboEntity entity);

    List<ComboListResponse> entityToListResponses(List<ComboEntity> entity);


    /**
     * 币种转化
     *
     * @param currency 币种
     * @return 币种
     */
    @Named("currencyConvert")
    default String currencyConvert(String currency) {
        Optional<CurrencyEnum> currencyEnum = CurrencyEnum.getBySymbol(currency);
        return currencyEnum.map(CurrencyEnum::getSign).orElse(null);
    }

    @Named("configConvert")
    default List<ComboConfigInfoVO> configConvert(String config) {
        if (StringUtils.isNotBlank(config)) {
            JSONArray jsonArray = JSONObject.parseObject(config).getJSONArray("config");
            return JSON.parseArray(JSON.toJSONString(jsonArray), ComboConfigInfoVO.class);
        }
        return Collections.emptyList();
    }
}
